package com.demo.forum.services.impl;

import com.demo.forum.common.Result;
import com.demo.forum.common.ResultCode;
import com.demo.forum.dao.ArticleMapper;
import com.demo.forum.model.Article;
import com.demo.forum.model.Board;
import com.demo.forum.model.User;
import com.demo.forum.model.exception.ApplicationException;
import com.demo.forum.services.IArticleService;
import com.demo.forum.services.IBoradService;
import com.demo.forum.services.IUserService;
import com.demo.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j

public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private IBoradService boradService;

    @Autowired
    private IUserService userService;

    @Autowired
   private ArticleMapper articleMapper;

    @Override
    public void create(Article article) {
        if(article == null || article.getBoardId() == null || article.getUserId() == null
             || StringUtil.isEmpty(article.getTitle())
            || StringUtil.isEmpty(article.getContent())){

            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        article.setDeleteState((byte) 0);
        article.setLikeCount(0);
        article.setReplyCount(0);
        article.setVisitCount(0);
        article.setState((byte) 0);
        Date date = new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);
        int articleRow = articleMapper.insertSelective(article);
        if(articleRow <= 0){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_CREATE));
        }
        //查询板块信息
        Board board = boradService.selectById(article.getBoardId());
        if(board == null ){
            log.warn(ResultCode.FAILED_CREATE.toString() + " 发帖失败 board id= "+ article.getBoardId());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_CREATE));
        }
        boradService.addOneArticleCountById(article.getBoardId());

        //查询用户信息
        User user = userService.selectById(article.getUserId());
        if(user == null ){
            log.warn(ResultCode.FAILED_CREATE.toString() + " 发帖失败 , user id =" + article.getUserId());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_CREATE));
        }
        userService.addOneArticleCountById(user.getId());


        log.info(ResultCode.SUCCESS+ " , user id= "+article.getUserId()
        + " , board id = " + article.getBoardId()  +" , article title = "+ article.getTitle()


        );



    }

    @Override
    public List<Article> selectAll() {
        List<Article> articles = articleMapper.selectAll();
        return articles;
    }

    @Override
    public List<Article> selectAllByBordId(Long boardId) {
        if(boardId == null || boardId <=0 ) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boradService.selectById(boardId);
        if(board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return articleMapper.selectAllByBordId(boardId);
    }

    @Override
    public Article selectDetailById(Long id) {
            if(id == null || id <= 0) {
                log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
                throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
            }
            Article article = articleMapper.selectDetailById(id);
            if(article == null) {
                log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
                throw new ApplicationException(Result.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
            }
            Article updateArticle = new Article();
            updateArticle.setId(id);
            updateArticle.setVisitCount(article.getVisitCount()+1);
            int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
            if(row != 1){
                log.warn(ResultCode.ERROR_SERVICES.toString());
                throw new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES));
            }
            article.setVisitCount(article.getVisitCount()+1);
            return article;
   }

    @Override
    public Article selectById(Long id) {
        if(id == null || id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void modify(Long id, String title, String content) {
        if(id == null || id<= 0 || StringUtil.isEmpty(title) || StringUtil.isEmpty(content)){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setTitle(title);
        updateArticle.setContent(content);

        updateArticle.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES));
        }

    }

    /**
     * 点赞
     * @param id 帖子id
     */
    @Override
    public void thumbsUp(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article =articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if(article.getState() == 1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setLikeCount(article.getLikeCount()+1);
        updateArticle.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES));
        }


    }

    @Override
    public void delete(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() ==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
         Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setDeleteState((byte) 1);
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES));
        }
        Board board = boradService.selectById(article.getBoardId());
        if(board == null ){
            log.warn(ResultCode.FAILED_CREATE.toString() + " 发帖失败 board id= "+ article.getBoardId());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_CREATE));
        }
        boradService.subOneArticleCountById(article.getBoardId());
        User user = userService.selectById(article.getUserId());
        if(user == null ){
            log.warn(ResultCode.FAILED_CREATE.toString() + " 发帖失败 user id= "+ article.getUserId());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_CREATE));
        }

        userService.subOneArticleCountById(article.getUserId());


    }

    /**
     * 回复帖子个数 +1
     * @param id 版块Id
     */

    @Override
    public void addOneReplyCountById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if(article.getState() == 1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount()+1);
        updateArticle.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES));
        }


    }

    @Override
    public List<Article> selectByUserId(Long userId) {
        if(userId == null || userId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user = userService.selectById(userId);
        //管理员可能进行相关操作,故不需要判断 deleteState ==1
        if(user == null ){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString()+ ", user id" + userId);
            throw new ApplicationException(Result.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        List<Article> articles = articleMapper.selectByUserId(userId);
        return articles;

    }


}
